package com.jiajia.study.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: RabbitDeadLetterExchangeV1Config
 * @Author zjj
 * @Package com.jiajia.study.config
 * @Date 2024/3/9 16:42
 * @description:    version 1 版本的 DLX
 *
 * 发送的是 ttl 的消息，过了消息存活时间，发送到 DLX 中，然后投递到 死信队列中
 *
 * 队列：正常队列，死信队列
 * 交换机：正常交换机，死信交换机
 */

@Configuration
public class RabbitDeadLetterExchangeV1Config {

    @Bean
    public DirectExchange directExchangeNormalV1(){
        return ExchangeBuilder.directExchange("dlx.exchange.normal.v1").build();
    }

    @Bean
    public DirectExchange directExchangeDlxV1(){
        return ExchangeBuilder.directExchange("dlx.exchange.dlx.v1").build();
    }

    @Bean
    public Queue queueNormalV1(){
        Map<String,Object> arguments = new HashMap<>();
        // 队列中的消息过期，将消息投递到的队列
        arguments.put("x-dead-letter-exchange","dlx.exchange.dlx.v1");
        //  队列中的消息过期，将消息投递到的队列时的路由key
        arguments.put("x-dead-letter-routing-key","error");
        return QueueBuilder.durable("dlx.queue.normal.v1").withArguments(arguments).build();
    }

    @Bean Queue queueDlxV1(){
        return QueueBuilder.durable("dlx.queue.dlx.v1").build();
    }

    @Bean
    public Binding bindingNormalV1(DirectExchange directExchangeNormalV1,Queue queueNormalV1){
        return BindingBuilder.bind(queueNormalV1).to(directExchangeNormalV1).with("normal");
    }

    @Bean
    public Binding bindingDlxV1(DirectExchange directExchangeDlxV1,Queue queueDlxV1){
        return BindingBuilder.bind(queueDlxV1).to(directExchangeDlxV1).with("error");
    }



}
